<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="12" :offset="6">
        <div style="text-align: center;margin-bottom: 30px">
          岗位{{ createStatus ? '新增' : '更新' }}
        </div>
        <el-form :model="buildingForm" ref="buildingForm" label-width="100px">
          <el-row :gutter="10">
            <el-form-item label="岗位名称" prop="postName">
              <el-input type="text" v-model="buildingForm.postName" placeholder="请输入岗位名称" clearable></el-input>
            </el-form-item>

            <el-form-item label="岗位编码" prop="postCode">
              <el-input v-model="buildingForm.postCode" placeholder="请输入岗位编码" clearable></el-input>
            </el-form-item>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelBuilding">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createBuilding">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updatePost">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {getInPostVo,addInPost,updateInPost} from "@/api/post";
import {getPostList} from "@/api/post";


export default {
  name: "postEdit",
  data() {
    return {
      buildingForm: {
        unitAbutSituation:[]
      },
      addLoading: false,

      postList:[],

    }
  },

  computed: {
    createStatus() {
      return this.$route.query.id == undefined
    },
    // enterPriseHidden(){
    //   let roleCode = this.$store.getters.roleCode;
    //   return roleCode=="XT-SUPER"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
    // }
  },
  created() {
    // this.getBuildingList()
    // this.getInEnterpriseDominantList()
    if (!this.createStatus) {
      let postName = this.$route.query.id        //根据id判断修改的对象
      this.getInPostVo(postName)
    }
  },
  methods: {
    getInPostVo(val) {
      getInPostVo({id: val}).then(res => {
        this.buildingForm = res.data
      }).catch(error => {

      })
    },
    cancelBuilding() {
      window.history.back()
    },
    createBuilding() {
      this.addLoading = true
      // setTimeout(()=>{},100000)
      this.$refs['buildingForm'].validate(valid => {
        if (valid) {
          let obj = Object.assign({},this.buildingForm)
          addInPost(obj).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加岗位成功"
            })
            this.cancelBuilding()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updatePost() {
      this.addLoading = true
      this.$refs['buildingForm'].validate(valid => {
        if (valid) {
          let obj = Object.assign({},this.buildingForm)
          console.log(obj)
          updateInPost(obj).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新岗位成功"
            })
            this.cancelBuilding()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    getPostList(){
      getPostList({deleted:false}).then(res=>{
        this.postList = res.data
      }).catch(error=>{

      })
    },


  }

}
</script>
